import { ref } from 'vue'
import { 
  getAssignmentList,
  updateTheRecheck,
  getRecheckPaperDetails,
  saveRecheck,
  handleRecheck,
 } from '@/api/server/recheckTask'
import { useServerUserStore } from '@/stores/modules/user-server'
import { useRouter } from 'vue-router'

export default function useRecheckTask() {

  const router = useRouter()
  const assignmentList = ref([])
  const total = ref(0)
  const page = ref(1)
  const pageSize = ref(10)
  const status = ref('所有状态')

  const userStore = useServerUserStore()

  const getAssignmentListOfHook = async () => {
    const res = await getAssignmentList({
        userId: userStore.userInfo.id,
        page: page.value  ,
        pageSize: pageSize.value,
        status: status.value
    })
    assignmentList.value = res.data.data.list
    total.value = res.data.data.total
  }

  const handleReset = () => {
    status.value = '所有状态'
    getAssignmentListOfHook()
  }
  
  const handleQuery = async () => {
    await getAssignmentListOfHook()
    if(assignmentList.value.length > 0) { 
      ElMessage.success('查询成功') 
    } else {
      ElMessage.warning('暂无数据')
    }
  }

  // 跳转复查界面
  const gotoRecheckComponent = (row) => {
    updateTheRecheck({
      recheckId: row.recheckId,
      status: 1
    })
    router.push({
      name: '复查界面',
      query: {
        recheckId: row.recheckId,
        recheckRequestId: row.recheckRequestId
      }
    })
  }

  // 获取复查任务详情
  const recheckPaperDetails = ref({})
  const getRecheckPaperDetailsOfHook = async (recheckId) => {
    const res = await getRecheckPaperDetails({
      recheckId
    })
    recheckPaperDetails.value = res.data.data
  } 

  // 保存复查任务
  const saveRecheckOfHook = async (item) => {
    const recheckDetailId = item.recheckDetailId
    const recheckDetailFinalScore = item.recheckDetailFinalScore

    const res = await saveRecheck({
      recheckDetailId,
      recheckDetailFinalScore
    })
    if(parseInt(res.data.code) === 200) {
      await getRecheckPaperDetailsOfHook(item.recheckId)
      ElMessage.success('保存成功')
    } else {
      ElMessage.error('保存失败')
    }
  }


  // 提交全部审查
  const handleRecheckOfHook = async (recheckId,recheckRequestId) => {
    if(recheckPaperDetails.value.doneTotal === recheckPaperDetails.value.total) {
      console.log('recheckId: ',recheckId)
      console.log('recheckRequestId: ',recheckRequestId)
      const res = await handleRecheck({
        recheckId,
        recheckRequestId
      })
      if(parseInt(res.data.code) === 200) {
        ElMessage.success('提交成功')
        router.go(-1)
      } else {
        ElMessage.error('提交失败')
      }

    } else {
      ElMessage.warning('请先完成所有审查')
    }
  }
 
  return {
    assignmentList,
    total,
    page,
    pageSize,
    status,
    getAssignmentListOfHook,
    handleReset,
    handleQuery,

    gotoRecheckComponent,

    // 获取复查任务详情
    recheckPaperDetails,
    getRecheckPaperDetailsOfHook,

    // 保存复查任务
    saveRecheckOfHook,

    // 提交全部审查
    handleRecheckOfHook
  }
}